﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Hangout.Client.Controls.Trophy
{
    public partial class TrophyList : UserControl
    {

       

       public  delegate void LoadComplated(TrophyService.GetUserTrophiesCompletedEventArgs e);

       public event LoadComplated TrophyLoadCompleted;


        public TrophyList()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private int userId;

        public int UserID
        {


            get
            {
                return userId;
            }
            set
            {
                userId = value;
                if (userId != 0)
                {
                    LoadData();
                }
            }
        }

        private void LoadData()
        {
            try
            {
                
                Services.TrophyServiceClient.GetUserTrophiesCompleted += new EventHandler<TrophyService.GetUserTrophiesCompletedEventArgs>(client_GetUserTrophiesCompleted);
                Services.TrophyServiceClient.GetUserTrophiesAsync(UserID, Settings.Settings.FacebookAccessToken);
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void client_GetUserTrophiesCompleted(object sender, TrophyService.GetUserTrophiesCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    if (e.Result.Count > 0)
                    {

                        TrophyLB.DataContext = e.Result.ToList();
                        TrophyLB.ItemsSource = e.Result.ToList();
                        ItemList.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {

                        NoLBL.Visibility = System.Windows.Visibility.Visible;

                    }
                }
                else
                {
                    //turbulance 
                    MessageBox.Show(ErrorText.Description, ErrorText.Caption, MessageBoxButton.OK);

                }

                if (TrophyLoadCompleted != null)
                {
                    TrophyLoadCompleted(e);
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }


    }
}
